package o;

import com.huawei.hms.aaid.constant.AaidIdConstant;
import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map.Entry;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class byz {
    private static final HashMap<String, String> ˋ = new HashMap();
    private static ExecutorService ˎ = Executors.newSingleThreadExecutor();
    private static final HashMap<String, String> ॱ = new HashMap();

    static {
        ˋ.put("FFD8FF", "jpeg");
        ˋ.put("89504E47", "png");
        ˋ.put("47494638", "gif");
        ˋ.put("524946", "webp");
        ॱ.put("jpg", ".jpg");
        ॱ.put("jpeg", ".jpeg");
        ॱ.put("png", ".png");
        ॱ.put("webp", ".webp");
        ॱ.put("gif", ".gif");
    }

    static boolean ˋ(byl o_byl, boolean z, String str) {
        if (o_byl == null) {
            bqx.ॱ("UploadImageUtil", "uploadImageData == null");
            return false;
        }
        try {
            if (!ˏ(o_byl, z, str)) {
                return false;
            }
            ˋ(o_byl);
            return true;
        } catch (Exception e) {
            bqx.ॱ("UploadImageUtil", "pretreatImage exception");
            return false;
        }
    }

    private static void ˋ(byl o_byl) {
        String ˎ = dnw.ˎ(o_byl.ˊ(), "MD5");
        o_byl.ॱ(ˎ == null ? "" : ˎ.toLowerCase(Locale.US));
        ˎ = dnw.ˎ(o_byl.ˊ(), AaidIdConstant.SIGNATURE_SHA256);
        o_byl.ˊ(ˎ == null ? "" : ˎ.toLowerCase(Locale.US));
    }

    /* JADX WARNING: inconsistent code. */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    private static boolean ˏ(o.byl r13, boolean r14, java.lang.String r15) {
        /*
        r3 = 0;
        r1 = 0;
        if (r13 != 0) goto L_0x000d;
    L_0x0004:
        r0 = "UploadImageUtil";
        r2 = "uploadImageData == null";
        o.bqx.ॱ(r0, r2);
        r0 = r1;
    L_0x000c:
        return r0;
    L_0x000d:
        r2 = r13.ᐝॱ();
        if (r2 == 0) goto L_0x0031;
    L_0x0013:
        r0 = ˎ(r14, r15);
    L_0x0017:
        r4 = new java.io.File;
        r4.<init>(r0);
        r0 = r4.exists();
        if (r0 != 0) goto L_0x0036;
    L_0x0022:
        r0 = r4.mkdirs();
        if (r0 != 0) goto L_0x0036;
    L_0x0028:
        r0 = "UploadImageUtil";
        r2 = "Fail to create path";
        o.bqx.ॱ(r0, r2);
        r0 = r1;
        goto L_0x000c;
    L_0x0031:
        r0 = ˏ(r14, r15);
        goto L_0x0017;
    L_0x0036:
        r0 = r13.ˏ();
        r4 = ˊ(r0);
        r5 = new java.io.File;
        r0 = r13.ˏ();
        r5.<init>(r0);
        if (r2 == 0) goto L_0x00bc;
    L_0x0049:
        r0 = new java.lang.StringBuilder;
        r0.<init>();
        r2 = ˎ(r14, r15);
        r0 = r0.append(r2);
        r2 = "/";
        r0 = r0.append(r2);
        r2 = r13.ˋ();
        r0 = r0.append(r2);
        r2 = ʼ(r4);
        r0 = r0.append(r2);
        r0 = r0.toString();
    L_0x0070:
        r6 = new java.io.BufferedInputStream;	 Catch:{ Exception -> 0x00b1 }
        r2 = new java.io.FileInputStream;	 Catch:{ Exception -> 0x00b1 }
        r2.<init>(r5);	 Catch:{ Exception -> 0x00b1 }
        r6.<init>(r2);	 Catch:{ Exception -> 0x00b1 }
        r2 = 0;
        r7 = new java.io.BufferedOutputStream;	 Catch:{ Throwable -> 0x00a4, all -> 0x0107 }
        r8 = new java.io.FileOutputStream;	 Catch:{ Throwable -> 0x00a4, all -> 0x0107 }
        r8.<init>(r0);	 Catch:{ Throwable -> 0x00a4, all -> 0x0107 }
        r7.<init>(r8);	 Catch:{ Throwable -> 0x00a4, all -> 0x0107 }
        r8 = 0;
        r9 = 1024; // 0x400 float:1.435E-42 double:5.06E-321;
        r9 = new byte[r9];	 Catch:{ Throwable -> 0x0096, all -> 0x0128 }
    L_0x008a:
        r10 = r6.read(r9);	 Catch:{ Throwable -> 0x0096, all -> 0x0128 }
        r11 = -1;
        if (r10 == r11) goto L_0x00e4;
    L_0x0091:
        r11 = 0;
        r7.write(r9, r11, r10);	 Catch:{ Throwable -> 0x0096, all -> 0x0128 }
        goto L_0x008a;
    L_0x0096:
        r0 = move-exception;
        throw r0;	 Catch:{ all -> 0x0098 }
    L_0x0098:
        r2 = move-exception;
        r12 = r2;
        r2 = r0;
        r0 = r12;
    L_0x009c:
        if (r7 == 0) goto L_0x00a3;
    L_0x009e:
        if (r2 == 0) goto L_0x0112;
    L_0x00a0:
        r7.close();	 Catch:{ Throwable -> 0x010d, all -> 0x0107 }
    L_0x00a3:
        throw r0;	 Catch:{ Throwable -> 0x00a4, all -> 0x0107 }
    L_0x00a4:
        r0 = move-exception;
        throw r0;	 Catch:{ all -> 0x00a6 }
    L_0x00a6:
        r2 = move-exception;
        r3 = r0;
        r0 = r2;
    L_0x00a9:
        if (r6 == 0) goto L_0x00b0;
    L_0x00ab:
        if (r3 == 0) goto L_0x0124;
    L_0x00ad:
        r6.close();	 Catch:{ Throwable -> 0x011f }
    L_0x00b0:
        throw r0;	 Catch:{ Exception -> 0x00b1 }
    L_0x00b1:
        r0 = move-exception;
        r0 = "UploadImageUtil";
        r2 = "copy image error";
        o.bqx.ॱ(r0, r2);
        r0 = r1;
        goto L_0x000c;
    L_0x00bc:
        r0 = new java.lang.StringBuilder;
        r0.<init>();
        r2 = ˏ(r14, r15);
        r0 = r0.append(r2);
        r2 = "/";
        r0 = r0.append(r2);
        r2 = r13.ˋ();
        r0 = r0.append(r2);
        r2 = ʼ(r4);
        r0 = r0.append(r2);
        r0 = r0.toString();
        goto L_0x0070;
    L_0x00e4:
        r13.ˏ(r0);	 Catch:{ Throwable -> 0x0096, all -> 0x0128 }
        r10 = r5.length();	 Catch:{ Throwable -> 0x0096, all -> 0x0128 }
        r13.ˋ(r10);	 Catch:{ Throwable -> 0x0096, all -> 0x0128 }
        r13.ˎ(r4);	 Catch:{ Throwable -> 0x0096, all -> 0x0128 }
        if (r7 == 0) goto L_0x00f8;
    L_0x00f3:
        if (r3 == 0) goto L_0x0109;
    L_0x00f5:
        r7.close();	 Catch:{ Throwable -> 0x0102, all -> 0x0107 }
    L_0x00f8:
        if (r6 == 0) goto L_0x00ff;
    L_0x00fa:
        if (r3 == 0) goto L_0x011b;
    L_0x00fc:
        r6.close();	 Catch:{ Throwable -> 0x0116 }
    L_0x00ff:
        r0 = 1;
        goto L_0x000c;
    L_0x0102:
        r0 = move-exception;
        r8.addSuppressed(r0);	 Catch:{ Throwable -> 0x00a4, all -> 0x0107 }
        goto L_0x00f8;
    L_0x0107:
        r0 = move-exception;
        goto L_0x00a9;
    L_0x0109:
        r7.close();	 Catch:{ Throwable -> 0x00a4, all -> 0x0107 }
        goto L_0x00f8;
    L_0x010d:
        r4 = move-exception;
        r2.addSuppressed(r4);	 Catch:{ Throwable -> 0x00a4, all -> 0x0107 }
        goto L_0x00a3;
    L_0x0112:
        r7.close();	 Catch:{ Throwable -> 0x00a4, all -> 0x0107 }
        goto L_0x00a3;
    L_0x0116:
        r0 = move-exception;
        r2.addSuppressed(r0);	 Catch:{ Exception -> 0x00b1 }
        goto L_0x00ff;
    L_0x011b:
        r6.close();	 Catch:{ Exception -> 0x00b1 }
        goto L_0x00ff;
    L_0x011f:
        r2 = move-exception;
        r3.addSuppressed(r2);	 Catch:{ Exception -> 0x00b1 }
        goto L_0x00b0;
    L_0x0124:
        r6.close();	 Catch:{ Exception -> 0x00b1 }
        goto L_0x00b0;
    L_0x0128:
        r0 = move-exception;
        r2 = r3;
        goto L_0x009c;
        */
        throw new UnsupportedOperationException("Method not decompiled: o.byz.ˏ(o.byl, boolean, java.lang.String):boolean");
    }

    public static void ˋ() {
        dnw.ˊ(new File(ˏ()));
    }

    public static void ˋ(final boolean z, final String str) {
        ˎ.execute(new Runnable() {
            public void run() {
                try {
                    dnw.ˊ(new File(z ? byz.ˎ(str) : byz.ˋ(str)));
                } catch (Exception e) {
                    bqx.ॱ("UploadImageUtil", "deleteFiles exception");
                }
            }
        });
    }

    private static String ˏ() {
        return bqy.ˋ().ˏ().getApplicationContext().getFilesDir().getAbsolutePath() + "/forum/";
    }

    private static String ˏ(boolean z, String str) {
        return z ? ˎ(str) : ˋ(str);
    }

    public static String ˎ(boolean z, String str) {
        return z ? ˏ(str) : ॱ(str);
    }

    public static String ˋ(String str) {
        return ˏ() + "comment" + "/" + str;
    }

    public static String ॱ(String str) {
        return ˏ() + "updatecomment" + "/" + str;
    }

    public static String ˎ(String str) {
        return ˏ() + "publish" + "/" + str;
    }

    public static String ˏ(String str) {
        return ˏ() + "updatepublish" + "/" + str;
    }

    private static String ˊ(String str) {
        String ʻ = ʻ(str);
        if (!dof.ˋ(ʻ)) {
            for (Entry entry : ˋ.entrySet()) {
                if (ʻ.toUpperCase(Locale.US).startsWith((String) entry.getKey())) {
                    String str2 = (String) entry.getValue();
                    if ("jpeg".equals(str2) && str.toLowerCase(Locale.US).endsWith("jpg")) {
                        return "jpg";
                    }
                    return str2;
                }
            }
        }
        return "jpg";
    }

    private static String ʼ(String str) {
        for (Entry entry : ॱ.entrySet()) {
            if (((String) entry.getKey()).equals(str)) {
                return (String) entry.getValue();
            }
        }
        return ".jpg";
    }

    private static String ʻ(String str) {
        Throwable th;
        String str2 = null;
        FileInputStream fileInputStream;
        try {
            fileInputStream = new FileInputStream(str);
            try {
                byte[] bArr = new byte[4];
                if (-1 != fileInputStream.read(bArr, 0, bArr.length)) {
                    str2 = doa.ˎ(bArr);
                }
                if (fileInputStream != null) {
                    try {
                        fileInputStream.close();
                    } catch (Exception e) {
                        bqx.ˏ("UploadImageUtil", "is close error,ignore");
                    }
                }
            } catch (Exception e2) {
                try {
                    bqx.ˋ("UploadImageUtil", "getFileHeader error");
                    if (fileInputStream != null) {
                        try {
                            fileInputStream.close();
                        } catch (Exception e3) {
                            bqx.ˏ("UploadImageUtil", "is close error,ignore");
                        }
                    }
                    return str2;
                } catch (Throwable th2) {
                    th = th2;
                    if (fileInputStream != null) {
                        try {
                            fileInputStream.close();
                        } catch (Exception e4) {
                            bqx.ˏ("UploadImageUtil", "is close error,ignore");
                        }
                    }
                    throw th;
                }
            }
        } catch (Exception e5) {
            fileInputStream = null;
            bqx.ˋ("UploadImageUtil", "getFileHeader error");
            if (fileInputStream != null) {
                fileInputStream.close();
            }
            return str2;
        } catch (Throwable th3) {
            Throwable th4 = th3;
            fileInputStream = null;
            th = th4;
            if (fileInputStream != null) {
                fileInputStream.close();
            }
            throw th;
        }
        return str2;
    }
}
